package com.arun.servceImpl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.arun.dao.ssm.YMDDao;
import com.arun.dto.C7mtpDTO;
import com.arun.entity.C7MTP;
import com.arun.entity.Result;
import com.arun.mappers.C7MTPMapper;
import com.arun.service.C7MPTService;

@Service
public class C7MPTServiceImpl implements C7MPTService{

    @Autowired
    private C7MTPMapper c7mtpMapper;
	@Autowired
	private YMDDao YMDMapper;
    
    @Value("${ymd.url}")
    private String url;
	@Resource(name = "httpClientTemplate")
	private RestTemplate httpClientTemplate;
	
	@Override
	public int saveC7MPT(C7MTP c7mtp) {
		// TODO Auto-generated method stub
		int result = 0;
		result = c7mtpMapper.addNewC7MTP(c7mtp);
		return result;
	}

	@Override
	
	public Result uploadSNAndMAC(C7mtpDTO c7mtpDto) throws RestClientException{
		// TODO Auto-generated method stub
		//String url = "http://device.immotor.com/import/go5";
	    HttpHeaders headers = new HttpHeaders();
	    headers.setContentType(MediaType.APPLICATION_JSON);
	    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
	    HttpEntity<C7mtpDTO> httpEntity = new HttpEntity<>(c7mtpDto, headers);
	    // ResponseEntity<String> entity = httpClientTemplate.getForEntity(url, String.class);

	    ResponseEntity<Result> responseEntity = httpClientTemplate.exchange(url, HttpMethod.POST, httpEntity, Result.class);
	    Result result = responseEntity.getBody();
	    
		return result;
	}

	@Override
	public List<C7MTP> queryAll() {
		// TODO Auto-generated method stub
		List<C7MTP> c7mtpList = c7mtpMapper.retrieveAll();
		return c7mtpList;
	}
	
	@Override
	public void checkSNFlow(Map<String, Object> mParam) {
		YMDMapper.checkSNFlow(mParam);
	}	

}
